package com.ikkong.system.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import com.eazytec.bpm.sysconfig.Config;
//import com.eazytec.common.kit.BeanUtils;
import com.jfinal.plugin.activerecord.Page;
import org.apache.commons.beanutils.BeanUtils;

public class CommonUtil {
	
	public static void createFieldNameList(List<Map<String, Object>> fieldNameList, String index, String width, String textAlign, String onRenderEvent, boolean isHidden) {
		Map<String, Object> fieldNameMap = new HashMap<String, Object>();
		fieldNameMap.put("index", index);
		fieldNameMap.put("width", width);
		fieldNameMap.put("textAlign", textAlign);
		fieldNameMap.put("isHidden", isHidden);
		fieldNameMap.put("onRenderEvent", onRenderEvent);
		fieldNameMap.put("isSort", true);
		fieldNameList.add(fieldNameMap);
	}
	
	public static void createEditFieldNameList(List<Map<String, Object>> fieldNameList, String index, String width, String textAlign, String onRenderEvent, boolean isHidden) {
		Map<String, Object> fieldNameMap = new HashMap<String, Object>();
		fieldNameMap.put("index", index);
		fieldNameMap.put("width", width);
		fieldNameMap.put("textAlign", textAlign);
		fieldNameMap.put("isHidden", isHidden);
		fieldNameMap.put("onRenderEvent", onRenderEvent);
		fieldNameMap.put("isSort", true);
		fieldNameMap.put("isEdit", true);
		fieldNameList.add(fieldNameMap);
	}

	public static void createFieldNameList(List<Map<String, Object>> fieldNameList, String index, String width, String textAlign, String onRenderEvent, boolean isHidden, boolean isSort) {
		Map<String, Object> fieldNameMap = new HashMap<String, Object>();
		fieldNameMap.put("index", index);
		fieldNameMap.put("width", width);
		fieldNameMap.put("textAlign", textAlign);
		fieldNameMap.put("isHidden", isHidden);
		fieldNameMap.put("onRenderEvent", onRenderEvent);
		fieldNameMap.put("isSort", isSort);
		fieldNameList.add(fieldNameMap);
	}
	
	public static PageResultBean convertPageBeanToPageResultBean(Page<?> pageBean, String[] fieldNames, Map<Object, Object> userdata) {

		PageResultBean pageResultBean = new PageResultBean();

		pageResultBean.setPage(pageBean.getPageNumber());
		pageResultBean.setRecords(pageBean.getTotalRow());
		pageResultBean.setTotal(pageBean.getTotalPage());
		pageResultBean.setUserdata(userdata);

		List<PageResultRow> rows = new ArrayList<PageResultRow>();

		PageResultRow row = null;
		if (pageBean.getList() != null) {
			for (Object object : pageBean.getList()) {
				row = new PageResultRow();
				List<Object> objects = new ArrayList<Object>();
				try {
					for (String fieldName : fieldNames) {
						Object fieldValue = BeanUtils.getSimpleProperty(object, fieldName);

						if (fieldName.equals("id")) {
							row.setId(fieldValue);
						}
						objects.add(fieldValue);
					}
				} catch (Exception e) {
					throw new RuntimeException("Error while convert PageBean To PageResultBean  for entitiy " + object.getClass(), e);
				}
				row.setCell(objects);
				rows.add(row);
			}
		}
		pageResultBean.setRows(rows);
		return pageResultBean;
	}

//	public static String getApplicationName(){
//		return Config.instance().get("application.name");
//	}
	
}
